package com.xyrl.project.common.service.impl;

import com.xyrl.project.common.service.RedisLockService;
import com.xyrl.starter.lock.redis.annotations.LockAction;
import com.xyrl.starter.lock.redis.lock.DistributedLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RedisLockServiceImpl implements RedisLockService {

    @Autowired
    private DistributedLock distributedLock;

    @Override
    public void redisLockTest() {
        boolean isLock = distributedLock.lock("zpaman", 10000L);
        if (isLock) {
            log.info("获得锁 方法休眠10 分钟");
            try {
                Thread.sleep(10 * 60 * 1000);
            } catch (Exception e) {
                log.error("休眠异常:", e);
            } finally {
                distributedLock.releaseLock("zpaman");
            }
        }
    }

    @Override
    @Async
    public void asyncNumber(int index) {
        log.info("进入异步线程个数测试方法:{}", index);
        try {
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    @LockAction(value = "zpaman:_#name_#pwd_#index")
    public void lockTest(String name, String pwd, int index) {

    }
}
